/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mintandleaf.webkits.core;

import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.name.Names;
import com.mintandleaf.webkits.data.WebKitsDataModel;
import com.mintandleaf.webkits.validator.WebKitsValidator;
import java.util.HashMap;
import java.util.Iterator;


/**
 *
 * @author teguhh
 */
public abstract class WebKitsModuleBinder implements Module{

    private HashMap<String,Class> moduleMap=new HashMap<String, Class>();
    private HashMap<String,Class> validatorMap=new HashMap<String, Class>();
    private HashMap<String,Class> dataModelMap=new HashMap<String, Class>();
    private HashMap<String,Class> objectMap=new HashMap<String, Class>();

    /**
     * Alway Overide this method to create module binder
     */
    public abstract void binding();

    @Override
    final public void configure(Binder paramBinder) {
        binding();
        //binding web module
        
        if(moduleMap.size()>0){
            Iterator<String> iterKey=moduleMap.keySet().iterator();
            while(iterKey.hasNext()){
                String key=iterKey.next();
                paramBinder.bind(WebKitsWebModule.class).annotatedWith(Names.named(key)).to(moduleMap.get(key));
            }
        }
        //binding validator
        if(validatorMap.size()>0){
            Iterator<String> iterKey=validatorMap.keySet().iterator();
            while(iterKey.hasNext()){
                String key=iterKey.next();
                paramBinder.bind(WebKitsValidator.class).annotatedWith(Names.named(key)).to(validatorMap.get(key));
            }
        }
        //binding other object
        if(objectMap.size()>0){
            Iterator<String> iterKey=objectMap.keySet().iterator();
            while(iterKey.hasNext()){
                String key=iterKey.next();
                paramBinder.bind(Object.class).to(objectMap.get(key));
            }
        }
        //binding data model
        if(dataModelMap.size()>0){
            Iterator<String> iterKey=dataModelMap.keySet().iterator();
            while(iterKey.hasNext()){
                String key=iterKey.next();
                System.out.println("Binding DataModel :" + key);
                paramBinder.bind(WebKitsDataModel.class).annotatedWith(Names.named(key)).to(dataModelMap.get(key));
            }
        }
        
        
    }

    /**
     * Binding url to a web module
     * @param urlPattern
     * @param webModule
     */
    final protected void mappingURL(String urlPattern,Class webModule){
        moduleMap.put(urlPattern, webModule);
    }
     
    /**
     * Binding validator
     * @param paramName
     * @param validatorClass
     */
    final protected void mappingValidator(String paramName,Class validatorClass){
        validatorMap.put(paramName, validatorClass);
    }

    /**
     * Binding datamodel
     * @param paramName 
     * @param data model class
     **/
    final protected void mappingModel(String paramName,Class modelClass){
        dataModelMap.put(paramName, modelClass);
    }
    
    /**
     * Maping and annotated value to an object
     * @param paramName
     * @param paramClass
     **/
     final protected void mapping(String paramName,Class paramClass){
        objectMap.put(paramName, paramClass);
    }

}

